﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Security.Cryptography;
using System.Text;

namespace wuliuback
{
    public partial class QueryInformation : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string fromCity = Request["from_city"];
            string toCity = Request["to_city"];
            string from_county = Request["from_county"];
            string to_county = Request["to_county"];
            string type = Request["type"]; //1为货源，2为车源,3为所有
            string pageSize=Request["page_size"];
            string minId=Request["min_id"];
            string user_id = Request["user_id"];
            string ids = Request["ids"];

            string code = "";
            string msg = "";
            string goods = "";
            string curMinId = "";

            String sql = "";
            DbHelper dbHelper = new DbHelper();

            if (type=="2")
            {
                sql = "select top (" + pageSize + ") *,truck_id as id,2 as type from t_truck   where status>-1  ";
                if (minId != null && minId.Length > 0)
                {
                    sql = sql + " and truck_id < " + minId;
                }
                if (fromCity != null && fromCity.Length > 0)
                {
                    sql = sql + " and from_city = '"+fromCity+"' ";
                }
                if (toCity != null && toCity.Length > 0)
                {
                    sql = sql + " and to_city = '" + toCity + "' ";
                }
                if (from_county != null && from_county.Length > 0)
                {
                    sql = sql + " and from_county = '" + from_county + "' ";
                }
                if (to_county != null && to_county.Length > 0)
                {
                    sql = sql + " and to_county = '" + to_county + "' ";
                }
                if (user_id != null && user_id.Length > 0)
                {
                    sql = sql + " and user_id = '" + user_id + "' ";
                }
                sql = sql + " order by created_time desc";
                string sql1 = "select min(t.truck_id) as min_id from (" + sql + ") t";


                DataSet dataSet = dbHelper.Select(sql1);
                DataTable table = dataSet.Tables[0];
                if (table.Rows.Count > 0)
                {
                    curMinId = table.Rows[0][0].ToString();
                }
            }
            else if (type == "1")
            {
                sql = "select top " + pageSize + " *,goods_id as id,1 as type from t_goods   where status>-1  ";
                if (minId != null && minId.Length > 0)
                {
                    sql = sql + " and goods_id < " + minId;
                }
                if (fromCity != null && fromCity.Length > 0)
                {
                    sql = sql + " and from_city = '" + fromCity + "' ";
                }
                if (toCity != null && toCity.Length > 0)
                {
                    sql = sql + " and to_city = '" + toCity + "' ";
                }
                if (from_county != null && from_county.Length > 0)
                {
                    sql = sql + " and from_county = '" + from_county + "' ";
                }
                if (to_county != null && to_county.Length > 0)
                {
                    sql = sql + " and to_county = '" + to_county + "' ";
                }
                if (user_id != null && user_id.Length > 0)
                {
                    sql = sql + " and user_id = '" + user_id + "' ";
                }
                sql = sql + " order by created_time desc";
                string sql1 = "select min(t.goods_id) from (" + sql + ") t";


                DataSet dataSet = dbHelper.Select(sql1);
                DataTable table = dataSet.Tables[0];
                if (table.Rows.Count > 0)
                {
                    curMinId = table.Rows[0][0].ToString();
                }
            }
            else
            {
                sql = "select top " + pageSize + " * from v_goods_truck  where status>-1 ";
                if (minId != null && minId.Length > 0)
                {
                    sql = sql + " and id < " + minId;
                }
                if (fromCity != null && fromCity.Length > 0)
                {
                    sql = sql + " and from_city = '" + fromCity + "' ";
                }
                if (toCity != null && toCity.Length > 0)
                {
                    sql = sql + " and to_city = '" + toCity + "' ";
                }
                if (from_county != null && from_county.Length > 0)
                {
                    sql = sql + " and from_county = '" + from_county + "' ";
                }
                if (to_county != null && to_county.Length > 0)
                {
                    sql = sql + " and to_county = '" + to_county + "' ";
                }
                if (user_id != null && user_id.Length > 0)
                {
                    sql = sql + " and user_id = '" + user_id + "' ";
                }
                if (ids != null && ids.Length > 0)
                {
                    sql = sql + " and id in (" + ids + ") ";
                }
               
                sql = sql + " order by created_time desc";
                string sql1 = "select min(t.id) from (" + sql + ") t";


                DataSet dataSet = dbHelper.Select(sql1);
                DataTable table = dataSet.Tables[0];
                if (table.Rows.Count > 0)
                {
                    curMinId = table.Rows[0][0].ToString();
                }
            }

            DataSet dataSet1 = dbHelper.Select(sql);
            DataTable table1 = dataSet1.Tables[0];
            
            code = "0";
            goods = JsonUtil.DataTableToJson(table1);

            Response.Output.WriteLine("{\"code\":\"" + code + "\",\"msg\":\"" + msg
                                    + "\",\"min_id\":\"" + curMinId + "\",\"page_size\":\"" + pageSize + "\",\"type\":\"" + type
                                    + "\",\"from_city\":\"" + fromCity + "\",\"to_city\":\"" + toCity + "\",\"info\":" + goods + "}");
        }
    }
}
